


# 定义一个简单的问候函数
def greet(name):
    return f"你好, {name}!"

# 调用函数
message = greet("Alice")
print(message)  # 输出: 你好, Alice!


def add(a,b):
    print("我正在努力做计算")
    c=a+b
    print(c)
    print("我已经计算完成了")
    return a+b
add(5,2)





def describe_pet(animal, name):
    print(f"我有一只{animal}，它的名字是{name}.")

describe_pet("狗", "小白")  # 输出: 我有一只狗，它的名字是小白.





describe_pet(name="小黑", animal="猫")  # 输出: 我有一只猫，它的名字是小黑.





def greet(name="朋友"):
    return f"你好, {name}!"

print(greet())       # 输出: 你好, 朋友!
print(greet("Bob"))  # 输出: 你好, Bob!





def func(a, b=1):  # 正确
    pass
def func(a=1, b):  # 错误，SyntaxError
    pass





def sum_all(*args):
    return sum(args)

print(sum_all(1, 2, 3,5))  # 输出: 6
print(sum_all(4, 5))     # 输出: 9





def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_info(name="Alice", age=20)  # 输出: name: Alice
                                  #       age: 20





def square(x):
    return x * x

result = square(4)
print(result)  # 输出: 16





def get_min_max(numbers):
    return min(numbers), max(numbers)
print(get_min_max([1, 2, 3, 4]))
min_val, max_val = get_min_max([1, 2, 3, 4])
print(min_val, max_val)  # 输出: 1 4





def change_value(x):
    x = 10  # 创建新对象，不影响外部
    print(x)

a = 5
change_value(a)
print(a)  # 输出: 5





def change_list(lst):
    lst.append(4)  # 修改原列表

my_list = [1, 2, 3]
change_list(my_list)
print(my_list)  # 输出: [1, 2, 3, 4]





def modify_list(lst):
    lst[0] = "changed"

my_list = [1, 2, 3]
modify_list(my_list)
print(my_list)  # 输出: ['changed', 2, 3]





def update_dict(d):
    d["new_key"] = "new_value"

my_dict = {"key": "value"}
update_dict(my_dict)
print(my_dict)  # 输出: {'key': 'value', 'new_key': 'new_value'}





def apply_func(func, value):
    return func(value)

def square(x):
    return x * x

result = apply_func(square, 4)
print(result)  # 输出: 16





def make_adder(n):
    def adder(x):
        return x + n
    return adder

add_5 = make_adder(5)
print(add_5(3))  # 输出: 8





square = lambda x: x * x
print(square(4))  # 输出: 16





numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x * x, numbers))
print(squares)  # 输出: [1, 4, 9, 16]





points = [(1, 2), (3, 1), (5, 0)]
sorted_points = sorted(points, key=lambda p: p[1])
print(sorted_points)  # 输出: [(5, 0), (3, 1), (1, 2)]






